from typing import List, Optional, Sequence
from pydantic import BaseModel, Field, validator
from schemas import _validator


class Option(BaseModel):
    text: str
    is_correct: int
    score: int


class QuestionBase(BaseModel):
    accesstoken: Optional[str] = None
    questionnaire_id: int
    title: str
    option_type: int
    img_path: Sequence[str]
    desc: str
    start_time: str
    end_time: str
    video_url: str
    video_source: int
    release_status: int = 0
    options: Sequence[Option]


class QuestionCreate(QuestionBase):
    answer_time: int = Field(..., alias="single_answer_time")
    # 转json
    container_json_encode = validator(
        "img_path", "options", allow_reuse=True)(_validator.container_json_encode)

    class Config:
        schema_extra = {
            "example": {
                          "accesstoken": "TOKEN3590",
                          "questionnaire_id": 1,
                          "title": "你对物业中心处理工作是否满意？",
                          "option_type": 0,
                          "img_path": [
                            "http://slave.homed.me/1.jpg",
                            "http://slave.homed.me/2.jpg"
                          ],
                          "desc": "调查业主对物业的满意度情况，以便改进工作",
                          "start_time": "2022-03-10 08:00:00",
                          "end_time": "2022-03-15 08:30:00",
                          "video_url": "http://127.0.0.1/",
                          "video_source": 1,
                          "single_answer_time": 120,
                          "options": [
                            {
                              "text": "A.满意",
                              "is_correct": 1,
                              "score": 2
                            },
                            {
                              "text": "B.不满意",
                              "is_correct": 0,
                              "score": 0
                            }
                          ]
                        }
            }


class QuestionAdjust(QuestionCreate):
    id: int
    answer_time: int = Field(..., alias="single_answer_time")

    class Config:
        schema_extra = {
            "example": {
                          "id": 1,
                          "accesstoken": "TOKEN3590",
                          "questionnaire_id": 1,
                          "title": "你对物业中心处理工作是否满意？",
                          "option_type": 0,
                          "img_path": [
                            "http://slave.homed.me/1.jpg",
                            "http://slave.homed.me/2.jpg"
                          ],
                          "desc": "调查业主对物业的满意度情况，以便改进工作",
                          "start_time": "2022-03-10 08:00:00",
                          "end_time": "2022-03-15 08:30:00",
                          "video_url": "http://127.0.0.1/",
                          "video_source": 1,
                          "single_answer_time": 120,
                          "options": [
                            {
                              "text": "A.满意",
                              "is_correct": 1,
                              "score": 2
                            },
                            {
                              "text": "B.不满意",
                              "is_correct": 0,
                              "score": 0
                            }
                          ]
                        }
            }


class QuestionDelete(BaseModel):
    id: List[int]


class Question(QuestionBase):
    id: int
    # 转 dict
    container_json_decode = validator("img_path", "options", pre=True, allow_reuse=True)(
        _validator.container_json_decode)
    # datetime 转换[数据库model数据转schema_model]
    datetime_str_decode = validator(
        "start_time", "end_time", pre=True, allow_reuse=True)(
        _validator.datetime_str_decode)

    class Config:
        orm_mode = True


if __name__ == '__main__':
    demo = {
              "accesstoken": "TOKEN3590",
              "questionnaire_id": 1,
              "title": "你对物业中心处理工作是否满意？",
              "option_type": 0,
              "img_path": [
                "http://slave.homed.me/1.jpg",
                "http://slave.homed.me/2.jpg"
              ],
              "desc": "调查业主对物业的满意度情况，以便改进工作",
              "start_time": "2022-03-10 08:00:00",
              "end_time": "2022-03-15 08:30:00",
              "video_url": "http://127.0.0.1/",
              "video_source": 1,
              "single_answer_time": 120,
              "options": [
                {
                  "text": "A.满意",
                  "is_correct": 1,
                  "score": 2
                },
                {
                  "text": "B.不满意",
                  "is_correct": 0,
                  "score": 0
                }
              ]
            }
    q = QuestionCreate(**demo)
    print(q)
